//
//  XXMacro.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/3/24.
//

import Foundation
import UIKit

// MARK: - 枚举
public enum XXAlertType{
    case fullScreen
    case pageSheet
    case formSheet
}

// MARK: - Block
public typealias XXVoidBlock = ()->Void
public typealias XXIntBlock = (Int)->Void
public typealias XXFloatBlock = (Float)->Void
public typealias XXStrBlock = (String)->Void
public typealias XXDicBlock = (Dictionary<String, Any>)->Void
public typealias XXArrBlock = (Array<Any>)->Void
public typealias XXAnyBlock = (Any)->Void

// MARK: - 获取屏幕 宽度、高度等尺寸
enum XXSizeType {
    case nav, tab, width, height, statusBar, tabSafeHeight, safeHeight
}
func get(height key:XXSizeType) ->CGFloat{
    switch key {
    case .nav:
        return CGFloat(statusBarHeight()+44)
    case .tab:
        return ((statusBarHeight() > 20) ? 83 : 49)
    case .statusBar:
        return CGFloat(statusBarHeight())
    case .tabSafeHeight:
        return ((kTabBarH == 83) ? 34 : 0)
    case .safeHeight:
        return 24
    case .width:
        return UIScreen.main.bounds.width
    case .height:
        return UIScreen.main.bounds.height
    }
}
let kNavH = get(height: .nav)
let kScreenW = get(height: .width)
let kScreenH = get(height: .height)
let kStatusBarH = get(height: .statusBar)
let kTabBarH = get(height: .tab)
let kTabBarSafeH = get(height: .tabSafeHeight)
let kSafeH = get(height: .safeHeight)
func statusBarHeight() -> Float {
    if #available(iOS 13.0, *) {
        let statusBarManager = UIApplication.shared.windows.first?.windowScene?.statusBarManager
        return Float(statusBarManager?.statusBarFrame.size.height ?? 20)
    } else {
        return Float(UIApplication.shared.statusBarFrame.size.height)
    }
}
let kScreenModeSize = UIScreen.main.currentMode?.size
public func kFrame(_ x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat) -> CGRect {
    return CGRect.init(x: x, y: y, width: w, height: h)
}
public func size(_ w: CGFloat, _ h: CGFloat) -> CGSize {
    return CGSize.init(width: w, height: h)
}
public func edgeInsets(_ top : CGFloat, _ left : CGFloat, _ bottom : CGFloat, _ right : CGFloat) -> UIEdgeInsets {
    return UIEdgeInsets.init(top: top, left: left, bottom: bottom, right: right)
}
public func point(_ x : CGFloat = 0, _ y : CGFloat = 0) -> CGPoint{
    return CGPoint(x: x,y: y)
}

// MARK: 判空
func isEmptyArr(arr:Any) -> Bool{
    if arr is [Any]{
        return (arr as! Array<Any>).isEmpty
    }
    return false
}

// MARK: - UserDefaults
let def = UserDefaults.standard
func get(_ key:String) ->Any {
    return def.object(forKey: key) as Any
}
func add(_ key:String ,_ obj:Any) {
    def.setValue(obj, forKey: key)
    def.synchronize()
}
func rm(_ key:String){
    def.removeObject(forKey: key)
    def.synchronize()
}
// MARK: - 获取系统对象
let application = UIApplication.shared
let appDelegate = UIApplication.shared.delegate

let appWindow = UIApplication.shared.keyWindow
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
// MARK: - 通知
let notCenter = NotificationCenter.default
func post(not name:String, _ userInfo:Dictionary<String,String> = [:]) {
    notCenter.post(name: Notification.Name(name), object: nil, userInfo: userInfo)
}
func add(obs observer: Any, _ name:String, _ selector:Selector){
    notCenter.addObserver(observer, selector: selector, name: Notification.Name(name), object: nil)
}
func rm(obs observer: Any){
    notCenter.removeObserver(observer)
}

// MARK: - 当前系统版本
public let sysVersion = Double(UIDevice.current.systemVersion) ?? 0.0
public let appVersion = (appBundleInfoVersion["CFBundleShortVersionString" as String] as? String ) ?? ""
public let appBundleInfoVersion = Bundle.main.infoDictionary ?? Dictionary()

// MARK: - 机型
let isPhone = device(is: .phone)
let isPhoneX = device(is: .phoneX)
let isPad = device(is: .pad)
enum XXDeviceType{
    case phone, pad, phoneX
}
func device(is type:XXDeviceType) -> Bool{
    switch type {
    case .phone:
        return UIDevice.current.userInterfaceIdiom == .phone ? true : false
    case .pad:
        return UIDevice.current.userInterfaceIdiom == .pad ? true : false
    case .phoneX:
        return statusBarHeight() == 44 ? true : false
    }
}
// MARK: - UUID
let UUID:String =  UIDevice.current.identifierForVendor?.uuidString.replacingOccurrences(of: "-", with: "") ?? "" // 103F98C4762846349594E02FA108A8C4
func getGUID()->String{
    return NSUUID().uuidString
}

// MARK: - 地区
let kLocation = "zh_CN"
public var isDebug: Bool {
    #if DEBUG
    return true
    #else
    return false
    #endif
}
public var isSimulator: Bool {
    #if targetEnvironment(simulator)
    return true
    #else
    return false
    #endif
}
// MARK: - 图片、颜色
let image = {(_ name:String) ->UIImage in
    return UIImage(named:name) ??  UIImage()
}
func kImage(_ name:String) -> UIImage {
    return UIImage(named:name) ??  UIImage()
}
func kUrl(_ url:String) -> URL? {
    return URL(string: url)
}
func XXlog<T>(_ message: T, file : String = #file, methodName: String = #function, lineNumber : Int = #line) {
    
    #if DEBUG
        let filePath = file as NSString
        let filePath_copy = filePath.lastPathComponent as NSString
        let fileName = filePath_copy.deletingPathExtension
        print("\nXSlog: \(fileName) : \(methodName) line: \(lineNumber)  \n\(message)")
    #endif
}
// MARK: 判空


